package functions.ems_claim;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;
import com.beans.AccountBean;
import com.beans.AdminUserBean;
import com.beans.EmpDeptOrProjectCostBean;
import com.beans.ProjectBean;
import com.beans.SubCategory;
import com.crc.mam.BaseCase;
import coms.Constants;
import coms.TestDateString;
import frame.HttpHelper;

public class ClaimLine extends BaseCase {
	private org.slf4j.Logger logger = LoggerFactory.getLogger(ClaimLine.class);

	String result;
	boolean isPassed = false;
	String amount;
	
	
  public boolean isPassed() {
    return isPassed;
  }
  public void setPassed(boolean isPassed) {
    this.isPassed = isPassed;
  }
  public String getAmount() {
    return amount;
  }
  public void setAmount(String amount) {
    this.amount = amount;
  }
  public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}

	/**
	 * 员工借款
	 * 返回报账明细行 lineId
	 * loanFindListUrl="/ems-claim/claimLine/findList"
	 */	
	public int findList(String claimId) {
	  
		logger.info("插入明细之后，通过"+claimId+ "查询报账明细行 -----start -------" );
	    int lineId = 0;
		String params = "{\"claimId\":" + claimId + "}";
		String url =Constants.LOAN_FIND_LIST_URL;
		try {
			result = HttpHelper.post(params, url);
			System.out.println("findList--------" + result);
			status = HttpHelper.isOk;
			if(status) {
			  try {
			    JSONArray array = new JSONArray(result);
			    JSONObject object = array.getJSONObject(0);
			    lineId = object.getInt("lineId");
			    remark = "[lineId="+lineId+"]";
			  }catch(JSONException e) {
			    e.printStackTrace();
			    remark = "JSON解析错误";
			  }
			}else {
			  try {
			    JSONObject object = new JSONObject(result);
			    remark = object.getString("message");
			  }catch(JSONException e) {
			    remark = "JSON解析错误";
			    e.printStackTrace();
			  }
			}
		} catch (Exception e) {
			e.printStackTrace();
			remark = "系统异常";
			status = false;
		}
		
		logger.info("插入明细之后，通过"+claimId+ "查询报账明细行 -----end -------" );
		return lineId;
	}
	
	/**
     * 员工借款
     * 返回报账明细行 lineId
     * loanFindListUrl="/ems-claim/claimLine/findList"
     */ 
    public int findTravelInfoList(String claimId) {
      
        logger.info("插入明细之后，通过"+claimId+ "查询报账明细行 -----start -------" );
        int lineId = 0;
        String params = "{\"claimId\":" + claimId + "}";
        String url =Constants.FIND_LIST_URL;
        try {
            result = HttpHelper.post(params, url);
            System.out.println("findList--------" + result);
            status = HttpHelper.isOk;
            if(status) {
              try {
                JSONArray array = new JSONArray(result);
                if(array.length() >0) {
                  JSONObject object  = array.getJSONObject(0);
                  remark = object.toString();
                }else {
                  remark = "插入不成功，查询不到数据";
                }
                
                
              }catch(JSONException e) {
                e.printStackTrace();
                remark = "JSON解析错误";
              }
            }else {
              try {
                JSONObject object = new JSONObject(result);
                remark = object.getString("message");
              }catch(JSONException e) {
                remark = "JSON解析错误";
                e.printStackTrace();
              }
            }
        } catch (Exception e) {
            e.printStackTrace();
            remark = "系统异常";
            status = false;
        }
        
        logger.info("插入明细之后，通过"+claimId+ "查询报账明细行 -----end -------" );
        return lineId;
    }
	
	/**
	 * 插入员工借款--报账明细
	 */
	public void insert(String claimId) {
		logger.info(" 插入员工借款--报账明细------- start -------------------");
		amount = "8888";
		String param = "{\"claimId\":"+claimId+"}";
		//System.out.println(param);
		String url = Constants.LOAN_INSERT_USER_URL;
		try {
			 result = HttpHelper.post( param, url);
			 status = HttpHelper.isOk;
			 logger.info("insert " + result);
			 if(status) {
			  
			 }else {
			   try {
			     JSONObject object = new JSONObject(result);
			     remark = object.getString("message");
			   }catch(JSONException e) {
			     remark = "JSON解析错误";
			   }
			 }
			
		}catch(Exception e) {
			e.printStackTrace();
			remark = "系统异常";
			status = false;
		}
		logger.info(" 插入员工借款--报账明细------- end -------------------");
	}
	
	public void saveRows(String lang,String lineId,String claimId,AdminUserBean userBean, EmpDeptOrProjectCostBean costBean, AccountBean accountBean,ProjectBean projectBean,String amount,SubCategory subCategory ) {
	   logger.info("saveRows--- start");
	   if(projectBean == null) {
	     projectBean = new ProjectBean(0, null, null, null, null,null, null, null);
	   }
	  String params = "[{\"lang\":\""+lang+"\",\"lineId\":"+lineId+",\"claimId\":"+claimId+","
	      + "\"costCenterCode\":\""+costBean.getCostCenterCode()+"\","
	      + "\"categoryCode\":null,\"subcategoryCode\":\""+subCategory.getSubCategoryCode()+"\","
	      + "\"accountItemCode\":\""+accountBean.getAccountItemCode()+"\",\"accountItemName\":\""+accountBean.getAccountItemName()+"\",\"childAccountItemCode\":\""+accountBean.getChildAccountItemCode()+"\","
	          + "\"childAccountItemName\":\""+accountBean.getChildAccountItemName()+"\","
	      + "\"currency\":null,\"currencyName\":null,"
	      + "\"projectCode\":\""+projectBean.getProjectCode()+"\",\"projectName\":\""+projectBean.getProjectName()+"\","
	      + "\"receptionCode\":null,\"receptionName\":null,\"entertainingDate\":null,\"regionalType\":null,\"peopleCount\":null,\"avgAmount\":null,"
	      + "\"applyAmount\":\""+amount+"\","
	          + "\"summary\":null,\"budgetDeptCode\":null,\"budgetProjectCode\":null,\"budgetMonth\":null,\"budgetAmount\":null,\"frozenAmount\":null,\"occupyAmount\":null,\"usableAmount\":null,\"crAccountItemCode\":null,"
	          + "\"repaymentDate\":\""+TestDateString.afterMonthDay(3)+"\","
	              + "\"createdBy\":\""+userBean.getUserCode()+"\",\"createdTime\":\""+TestDateString.now()+"\","
	                  + "\"updatedBy\":\""+userBean.getUserCode()+"\",\"updatedTime\":\""+TestDateString.now()+"\","
	                      + "\"productValue\":null,\"productValueDes\":null,\"areaValue\":null,\"areaValueDes\":null,\"sbuValue\":null,\"sbuValueDes\":null,\"relatedValue\":null,\"relatedValueDes\":null,\"zjChange\":null,\"zjChangeDes\":null,"
	                      + "\"creditAmount\":null,\"employeeNo\":\""+userBean.getEmpNo()+"\",\"employeeName\":\""+userBean.getUserName()+"\",\"feeDeptCode\":\""+costBean.getOrgCode()+"\",\"feeDeptName\":\""+costBean.getOrgName()+"\",\"electronicsInvoiceNo\":null,\"remark\":\"报账申请\",\"refLineId\":null,\"refHeadCode\":null,"
	                      + "\"subCategoryName\":\""+subCategory.getSubCategoryName()+"\","
	                      + "\"costCenterName\":\""+costBean.getCostCenterName()+"\",\"claimProcessWay\":null,\"superClaimProcessWay\":null,\"jiefangAuto\":true}]";
	   logger.info("params : " + params);
	   String url = Constants.SAVE_ROWS_URL;
	   try {
	     result = HttpHelper.post(params, url);
	     status = HttpHelper.isOk;
	     logger.info("saveRows " + result);
	     if(status) {
	       
	     }else {
	       JSONObject object = new JSONObject(result);
	       remark = object.getString("message");
	     }
	   }catch(Exception e) {
	     e.printStackTrace();
	     remark = "系统异常";
	     status = false;
	   }
	   logger.info("saveRows--- end");
	 }

}
